<template>
  <div class="container">
    <el-table :data="somedata" style="width: 100%" border>
      <el-table-column prop="location" label="景区地点" width="100">
      </el-table-column>
      <el-table-column prop="city" label="景区城市" width="100">
      </el-table-column>
      <el-table-column prop="title" label="景区标题" width="180">
      </el-table-column>
      <el-table-column prop="imglist" label="景区图片" width="150">
        <template slot-scope="scope">
          <img
            :src="scope.row.imglist[0]"
            alt="图片"
            width="150"
            height="150"
          />
        </template>
      </el-table-column>
      <el-table-column prop="content" label="景区介绍">
        <template slot-scope="scope">
          <div
            v-html="scope.row.content"
            style="height: 100px; overflow: hidden"
          ></div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180px">
        <template slot-scope="{ row }">
          <el-button
            type="success"
            icon="el-icon-zoom-in"
            size="mini"
            title="浏览"
            @click="watchdetail(row)"
          ></el-button>
          <el-button
            type="warning"
            icon="el-icon-edit"
            size="mini"
            title="编辑"
            @click="updateList(row)"
          ></el-button>

          <el-button
            slot="reference"
            type="danger"
            icon="el-icon-delete"
            size="mini"
            title="删除"
            @click="deleteList(row)"
            style="margin-left: 10px"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      background
      style="margin-top: 20px; text-align: center"
      :current-page="page"
      :total="total"
      :page-size="limit"
      :pager-count="7"
      :page-sizes="[3, 5, 10]"
      layout="prev, pager, next, jumper,->,sizes,total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
    <el-dialog
      title="景区详情"
      :visible.sync="dialogTableVisible"
      width="800px"
    >
      <div class="inbox">
        <div style="text-align: center; font-size: 20px; font-weight: 800">
          {{ nowitem.location }}
        </div>
        <div class="newcontent" v-html="nowitem.content"></div>
        <el-carousel
          :interval="4000"
          type="card"
          height="200px"
          style="margin-left: 100px; margin-top: 80px"
        >
          <el-carousel-item v-for="item in nowitem.imglist" :key="item">
            <img :src="item" style="width: 200px; height: 200px" />
          </el-carousel-item>
        </el-carousel>
        <el-divider>End.</el-divider>
      </div>
    </el-dialog>
    <el-dialog
      title="景区信息修改"
      :visible.sync="drawer"
      :before-close="handleClose"
      style="height: 100vh"
      width="1000px"
    >
      <div class="modify">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="景区地点">
            <el-input v-model="form.location" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="景区城市">
            <el-input v-model="form.city" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="景区标题">
            <el-input
              type="textarea"
              v-model="form.title"
              maxlength="30"
              show-word-limit
              :autosize="{ minRows: 2, maxRows: 4 }"
              style="width: 500px"
            ></el-input>
          </el-form-item>
          <el-form-item label="景区照片" style="width: 100%; height: 100%">
            <span
              v-for="(item, index) in form.imglist"
              :key="index"
              style="margin-left: 20px"
              @click="deleteimg(index)"
            >
              <img :src="item" style="width: 200px; height: 200px" />
            </span>
            <el-upload
              class="avatar-uploader"
              action="http://127.0.0.1:2024/upload"
              :on-success="handleAvatarSuccess"
              :show-file-list="false"
              :on-remove="handleRemove"
            >
              <i
                class="el-icon-plus avatar-uploader-icon"
                style="width: 200px; height: 200px"
              ></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="景区介绍">
            <fullcontent v-model="form.content"></fullcontent>
          </el-form-item>
        </el-form>

        <div class="demo-drawer__footer">
          <el-button @click="cancelForm">取 消</el-button>
          <el-button
            type="primary"
            @click="$refs.drawer.closeDrawer()"
            :loading="loading"
            >{{ loading ? "提交中 ..." : "确 定" }}</el-button
          >
        </div>
      </div>
    </el-dialog>
  </div>
</template>
  
<script>
import { gethotdata, deletehotdata } from "@/api/hot.js";
import fullcontent from "@/components/fullcontent";
components: {
  fullcontent;
}
export default {
  name: "Login",
  components: {
    fullcontent,
  },
  data() {
    return {
      hotadata: [],
      page: 1, //分页器当前第几页
      limit: 5, //每一页需要展示几条数据
      total: 0,
      somedata: [],
      nowitem: {},
      dialogTableVisible: false,
      drawer: false,
      form: {},
      dialogImageUrl: "",
      dialogVisible: false,
    };
  },
  mounted() {
    this.initdata();
  },
  methods: {
    async initdata() {
      let result = await gethotdata();
      // 假设 arr 是你要遍历的数组
      for (let i = 0; i < result.data.length; i++) {
        // 在循环中对数组中的元素进行操作
        result.data[i].imglist = JSON.parse(result.data[i].imglist); // 这里的 modifyElement 是你对元素进行修改的函数
      }
      if (result.status == 0) {
        this.hotadata = result.data;
        this.total = this.hotadata.length;
        this.somedata = this.hotadata.slice(
          (this.page - 1) * this.limit,
          (this.page - 1) * this.limit + this.limit
        );
        this.$store.dispatch("hot/savedata", this.hotadata);
      }
    },
    handleCurrentChange(page) {
      this.page = page;
      this.somedata = this.hotadata.slice(
        (this.page - 1) * this.limit,
        (this.page - 1) * this.limit + this.limit
      );
    },
    handleSizeChange(limit) {
      this.page = 1;
      this.limit = limit;
      this.somedata = this.hotadata.slice(
        (this.page - 1) * this.limit,
        (this.page - 1) * this.limit + this.limit
      );
    },
    watchdetail(row) {
      this.nowitem = row;
      this.dialogTableVisible = true;
    },
    async deleteList(row) {
      this.$confirm("是否删除该课程, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let result = await deletehotdata(row.id);
          if (result.status == 0) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.initdata();
          } else {
            this.$message.error(result.message);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async updateList(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.drawer = true;
    },
    handleClose() {
      this.drawer = false;
      this.form = {};
    },

    // 图片上传成功返回图片地址
    handleAvatarSuccess(res, file) {
      // 如果上传成功
      if (res) {
        console.log(res);
        this.form.imglist.push(res.imagePath);
      } else {
        ElMessage({
          message: "提交失败！",
          type: "error",
        });
      }
    },
    deleteimg(index) {
      this.$confirm("是否删除该照片, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.form.imglist.splice(index, 1); // 从下标index开始，删除1个元素
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
  
<style scoped>
.container {
  margin-left: 50px;
}
.modify {
  margin-left: 10px;
}
</style>
  
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  display: inline-block;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  display: inline-block;
}
.avatar {
  width: 178px;
  height: 178px;
  display: inline-block;
}
</style>